package com.hm.handler.request.hand;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

import com.alibaba.fastjson.JSONObject;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.common.RemoteClientUtil;
import com.hm.common.util.RequestLogDef;
import com.hm.system.mongo.domain.log.RequestPlanLog;
import com.hm.system.share.service.mongo.log.RequestPlanLogService;

/**
 * @author shishun.wang
 * @date 2016年5月13日 上午12:18:52
 * @version 1.0
 * @describe 
 */
@Component
public class RequestRecordHandler {
	
	@Autowired
	private RequestPlanLogService requestPlanLogService;

	public void handler(HttpServletRequest request,HandlerMethod method){
		RequestPlanAnnotation annotation = method.getMethod().getAnnotation(RequestPlanAnnotation.class);
		if (null != annotation) {
			RequestPlanLog requestPlanLog = new RequestPlanLog();
			{
				requestPlanLog.setTitle(annotation.title());
				requestPlanLog.setContent(annotation.content());
				requestPlanLog.setRequestMethod(Enum.valueOf(RequestLogDef.class, request.getMethod()));
				requestPlanLog.setRemoteIpAddress(RemoteClientUtil.getPackHost(request));
				requestPlanLog.setRequestUrl(request.getServletPath());
				requestPlanLog.setRequestParameter(JSONObject.toJSONString(request.getParameterMap()));
			}
			requestPlanLogService.addRequestPlanLog(requestPlanLog);
		}
	}
	
}
